package com.retired_serviceman_management.controller;

import com.retired_serviceman_management.entity.AdminInfo;
import com.retired_serviceman_management.entity.EnterpriseInfo;
import com.retired_serviceman_management.entity.VocationalInfo;
import com.retired_serviceman_management.service.Admin_Info_Service;
import com.retired_serviceman_management.service.Enterprise_Info_Service;
import com.retired_serviceman_management.service.Soldier_Info_Service;
import com.retired_serviceman_management.service.VocationalInfoService;
import com.retired_serviceman_management.util.Enterprise_util;
import com.retired_serviceman_management.util.ListUtil;
import com.retired_serviceman_management.util.Result;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("admin_info")
public class Admin_Info_Controller {

    @Resource
    private Admin_Info_Service admin_info_service;

    @Resource
    private Soldier_Info_Service soldier_info_service;

    @Resource
    private Enterprise_Info_Service enterprise_info_service;
    @Resource
    private VocationalInfoService vocationalInfoService;
    /*
    管理员登录方法
     */
    @ResponseBody
    @RequestMapping(value = "login",method = RequestMethod.POST)
//    public Result admin_login(@RequestParam("admAccount")String admAccount, @RequestParam("admPassword") String admPassword,HttpServletRequest request) {
    public Result admin_login(@RequestBody AdminInfo adminInfo, HttpServletRequest request) {
        int checkAccount = admin_info_service.CheckAccount(adminInfo.getAdmAccount(),adminInfo.getAdmPassword());
        if (checkAccount == 0) {//账号密码都正确
            return new Result("0000","欢迎登陆");

        } else if (checkAccount == 1) {//账号不存在
            return new Result("0001","账号不存在");
        } else {//账号存在，密码错误
            return new Result("0002","密码错误");
        }
    }
    /*
    管理员查看所有退役军人信息
    */

    @RequestMapping(value = "View_Soldier",method = RequestMethod.GET)
    public String View_Soldier( HttpServletRequest request){
        List<ListUtil> soldierInfo = soldier_info_service.queryAll();
        request.setAttribute("soldierInfo",soldierInfo);
        return "/jsp/see_soldier.jsp";
    }

    /**
     * 管理员增加退役军人账号信息
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insert_Soldier",method = RequestMethod.POST)
    public Result insert_Soldier(@RequestBody ListUtil listUtil,HttpServletRequest request){
        System.out.println("listUtil = " + listUtil);
        Integer integer = soldier_info_service.insert_Soldier(listUtil);
        System.out.println("integer = " + integer);
        return getResult(integer);
    }

    @Nullable
    private Result getResult(Integer integer) {
        if(integer == 0) return new Result("0000","添加失败，请重新输入");
        else if(integer == 1) return new Result("0001","添加成功");
        else if(integer == 2) return new Result("0002","请输入信息");
        else if(integer == 3) return new Result("0003","账号已存在，请重新输入");
        return null;
    }

    /*
    管理员修改退役军人信息
     */
    @ResponseBody
    @RequestMapping(value = "Update_Soldier",method = RequestMethod.POST)
    public Result Update_Soldier(@RequestBody ListUtil listUtil){
        System.out.println("soldierInfo = " + listUtil);
        Integer i = soldier_info_service.Update_Soldier(listUtil);
        if (i == 0){
            return new Result("0000","修改失败");
        }
        else return new Result("0001","修改成功");
    }

    /*
    管理员删除退役军人信息
     */
    @ResponseBody
    @RequestMapping(value = "Delete_Soldier",method = RequestMethod.POST)
    public Result Delete_Soldier(@RequestParam("mobile") String mobile, HttpSession session){
        System.out.println("mobile = " + mobile);
        Integer i = soldier_info_service.Delete_Soldier(mobile);
        System.out.println("i = " + i);
        if(i == 0){
            return new Result("0010","删除失败");
        }
        else
            return new Result("0011","删除成功");
    }
    /*
    管理员查看企业信息
     */
    @RequestMapping(value = "View_Enterprise",method = RequestMethod.GET)
    public String View_Enterprise(HttpServletRequest request){
        List<EnterpriseInfo> enterpriseInfos = enterprise_info_service.queryAll();
        request.setAttribute("enterpriseInfos",enterpriseInfos);
        return "/jsp/see_enterprise.jsp";
    }
    /**
     * 管理员增加企业信息
     */
    @ResponseBody
    @RequestMapping(value = "insert_Enterprise",method = RequestMethod.POST)
    public Result insert_Enterprise(@RequestBody Enterprise_util enterpriseInfo, HttpServletRequest request){
        System.out.println("enterpriseInfo = " + enterpriseInfo);
        Integer integer = enterprise_info_service.insert_Enterprise(enterpriseInfo);
        return getResult(integer);
    }
    /**
     * 管理员修改企业信息
     */
    @ResponseBody
    @RequestMapping(value = "Update_Enterprise",method = RequestMethod.POST)
    public Result Update_Enterprise(@RequestBody EnterpriseInfo enterpriseInfo){
        Integer i = enterprise_info_service.Update_Enterprise(enterpriseInfo);
        if (i == 0){
            return new Result("0100","修改失败");
        }
        if(i == 2){
            return new Result("0102","没有发生任何改变",enterpriseInfo.getComAccount());
        }
        else return new Result("0101","修改成功");
    }
    /**
     * 管理员删除企业信息
     */
    @ResponseBody
    @RequestMapping(value = "Delete_Enterprise",method = RequestMethod.POST)
    public Result Delete_Enterprise(@RequestParam("comId") Integer comId,HttpSession session){
        Integer i = enterprise_info_service.Delete_Enterprise(comId);
        if(i == 0){
            return new Result("0110","删除失败");
        }
        else return new Result("0111","删除成功");
    }
    /**
     * 管理员查看所有通过与待审核的信息
     */

    @RequestMapping(value = "view_vocation",method = RequestMethod.GET)
    public String view_vocation(HttpServletRequest request){
        List<VocationalInfo> vocationalInfos = vocationalInfoService.queryByStatus();
        System.out.println("vocationalInfos = " + vocationalInfos);
        request.setAttribute("vocationalInfos",vocationalInfos);
        return "/jsp/vocation_info.jsp";
    }

    /**
     * 查看职业信息详情
     */
    @RequestMapping(value = "view_detail/{comAccount}",method = RequestMethod.GET)
    public String view_detail(@PathVariable("comAccount") String comAccount,HttpServletRequest request){
        VocationalInfo vocationalInfo = vocationalInfoService.view_detail(comAccount);
        request.setAttribute("vocationalInfo",vocationalInfo);
        return "/jsp/view_detail.jsp";
    }
    /**
     * 管理员审核通过职业信息
     */
    @ResponseBody
    @RequestMapping(value = "Agree_vocation",method = RequestMethod.POST)
    public Result Agree_vocation(@RequestParam("infoId") Integer infoId){
        VocationalInfo queryById = vocationalInfoService.queryById(infoId);
        if(queryById.getInfoStatus().equals("T")){
            return new Result("1012","审核已通过，请勿重复操作");
        }
        Integer integer = vocationalInfoService.Agree_vocation(infoId);
        if(integer==1) return new Result("1011","通过审核成功");
        else return new Result("1001","通过审核失败");
    }

    /**
     * 管理员审核不通过职业信息
     */
    @ResponseBody
    @RequestMapping(value = "Disagree_vocation",method = RequestMethod.POST)
    public Result Disagree_vocation(@RequestParam("infoId") Integer infoId){
        VocationalInfo queryById = vocationalInfoService.queryById(infoId);
        if(queryById.getInfoStatus().equals("T")){
            return new Result("1012","审核已通过，请勿重复操作");
        }
        Integer integer = vocationalInfoService.Disagree_vocation(infoId);
        if(integer==1) return new Result("1011","不通过审核成功");
        else return new Result("1001","不通过审核失败");
    }

}
